{include file="common/header"}
<link rel="stylesheet" type="text/css" href="/themes/template/static/css/element.min.css">
<script type="text/javascript" src="/themes/template/static/js/vue.min.js"></script>
<script type="text/javascript" src="/themes/template/static/js/element.min.js"></script>
<div id="app" v-cloak>
    <div class="el-warp">
        <div class="el-user">
            {include file="user/site"}
            <el-card class="box-card content">
                <div slot="header" class="search">
                    <el-date-picker
                        size="small"
                        v-model="search.date"
                        type="daterange"
                        align="right"
                        unlink-panels
                        range-separator="至"
                        start-placeholder="开始日期"
                        end-placeholder="截止日期"
                        format="yyyy-MM-dd"
                        value-format="yyyy-MM-dd"
                        :picker-options="pickerOptions">
                    </el-date-picker>
                    <el-input style="width: 200px" size="small" @keyup.enter.native="searchList" placeholder="搜索" v-model="search.keyword"></el-input>
                    <el-button type="primary" size="small" @click="searchList()">搜索</el-button>
                </div>
                <div class="cms-list" v-infinite-scroll="list" :infinite-scroll-disabled="disabled">
                    <el-table ref="table" :data="data" @selection-change="selectionChange" :row-class-name="rowClass">
                        <el-table-column type="selection" width="55" :selectable="selectionDisabled"></el-table-column>
                        <el-table-column>
                            <template slot-scope="scope">
                                <div class="item">
                                    <div class="left">
                                        <a :href="scope.row.product.url" target="_blank"><img :src="scope.row.product.cover"></a>
                                        <div class="details">
                                            <a :href="scope.row.product.url" target="_blank" class="title">{{scope.row.product.title}}</a>
                                            <div class="ctitle">
                                                {{scope.row.product.sku}}
                                                <span class="remove" v-if="scope.row.id !== ''" @click="remove(scope.row)">删除</span>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="right">
                                        <template v-if="scope.row.id !== ''">
                                            <template v-if="scope.row.price != null">
                                                <div class="tag">
                                                    <el-input-number v-model="scope.row.number" :min="1" :max="999" size="mini" @change="aas(scope.row)"></el-input-number>
                                                </div>
                                                <div class="ctag">
                                                    总计：{{scope.row.number * scope.row.price}}元
                                                </div>
                                            </template>
                                            <img v-else src="/plugins/cms/static/index/images/failed.png">
                                        </template>
                                        <img v-else src="/plugins/cms/static/index/images/delete.png">
                                    </div>
                                </div>
                            </template>
                        </el-table-column>
                    </el-table>
                    <div v-if="loading" class="el-loader">加载中...</div>
                    <div v-if="noMore && loading === false" class="el-nomore">没有更多啦</div>
                    <div class="foot"></div>
                </div>
                <div class="cms-foot">
                    <el-button type="warning" size="small" :disabled="rows.length === 0">
                        结算<span>{{price}}元</span>
                    </el-button>
                    <el-button type="danger" size="small" @click="remove()" :disabled="rows.length === 0">删除</el-button>
                </div>
            </el-card>
        </div>
    </div>
</div>
<script type="text/javascript">
    new Vue({
        el: '#app',
        data() {
            return {
                rows: [],
                data: [],
                search:{
                    page: 1,
                    date: [],
                    keyword: "",
                },
                loading: false,
                pickerOptions: {
                    shortcuts: [{
                        text: '最近一周',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            end.setTime(start.getTime() + 3600 * 1000 * 24 * 1);
                            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
                            picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '最近一个月',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            end.setTime(start.getTime() + 3600 * 1000 * 24 * 1);
                            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
                            picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '最近三个月',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            end.setTime(start.getTime() + 3600 * 1000 * 24 * 1);
                            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
                            picker.$emit('pick', [start, end]);
                        }
                    }]
                },
            }
        },
        computed: {
            noMore() {
                return this.count <= this.data.length;
            },
            disabled() {
                return this.loading || this.noMore;
            },
            price() {
                let price = 0;
                this.rows.forEach(function (item, index) {
                    price += item.number * (item.price*100) / 100;
                })
                return price;
            },
        },
        methods: {
            /**
             * 加载列表
             */
            list() {
                let self = this;
                self.loading = true;
                post('api/cms/userProductCart/index', self.search, function (res) {
                    if (res.status === 'success') {
                        self.search.page++;
                        self.data  = self.data.concat(res.data);
                        self.count = res.count;
                    }
                    self.loading = false;
                })
            },
            /**
             * 搜索
             */
            searchList() {
                this.search.page = 1;
                this.data = [];
                this.list();
            },
            /**
             * 选择
             */
            selectionChange(val) {
                this.rows = val;
            },
            /**
             * 禁止选择
             */
            selectionDisabled(row, index) {
                return row.price != null;
            },
            /**
             * 删除
             */
            remove(row = "") {
                let self = this;
                self.$confirm('确定删除吗？', '', {confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning'}).then(() => {
                    let rows = row === "" ? self.rows : [row];
                    post('api/cms/userProductCart/delete', {ids: common.arrayColumn(rows, 'id')}, function (res) {
                        if (res.status === 'success') {
                            self.$refs.table.clearSelection();
                            rows.forEach(function (item, index) {
                                item.id = '';
                            })
                        }
                        self.$notify({message: res.message, type: res.status});
                    })
                }).catch(() => {});
            },
            rowClass({row, rowIndex}) {
                if (row.id == "") {
                    return 'delete-row';
                } else if (row.price == null) {
                    return 'failed-row';
                }
                return '';
            },
            /**
             * 加减
             */
            aas(item) {
                let self = this;
                post('api/cms/userProductCart/aas', item, function (res) {})
            },
        },
    });
</script>
</body>
</html>